local fudi = fk.CreateSkill{
  name = "swis__fudi"
}

Fk:loadTranslationTable{
  ["swis__fudi"] = "附敌",
  [":swis__fudi"] = "出牌阶段限一次，你可以将一张手牌交给一名其他角色并选择：1.摸两张牌，2.对其造成1点伤害；"
  .."当你受到伤害后，你可以对伤害来源发动此技能，且摸牌、伤害数翻倍。",

  ["#swis__fudi-prompt"] = "附敌：可以交给一名其他角色一张手牌，摸两张牌或对其造成1点伤害。",
  ["#swis__fudi-trigger"] = "附敌：可以交给 %src 一张手牌，摸四张牌或对其造成2点伤害。",
  ["#swis__fudi-choose"] = "附敌：请选择一项",
  ["#swis__fudi-draw"] = "摸牌",
  ["#swis__fudi-damage"] = "对 %src 造成伤害",

  ["$swis__fudi1"] = "弃暗投明，为明公计！",
  ["$swis__fudi2"] = "绣虽有降心，奈何贵营难容。",
}

---@param room Room
---@param player ServerPlayer
---@param target ServerPlayer
---@param card integer
---@param double boolean?
local fudi_effect = function (room, player, target, card, double)
  room:obtainCard(target, card, false, fk.ReasonGive, player, fudi.name)
  if player:isAlive() then
    local all_choices = {"#swis__fudi-draw", "#swis__fudi-damage:"..target.id}
    local choices = table.simpleClone(all_choices)
    if target.dead then
      table.remove(choices, 2)
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      all_choices = all_choices,
      cancelable = false,
      skill_name = fudi.name,
      prompt = "#swis__fudi-choose"
    })
    if choice == all_choices[1] then
      player:broadcastSkillInvoke(fudi.name, 1)
      room:notifySkillInvoked(player, fudi.name, "drawcard")
      player:drawCards(double and 4 or 2, fudi.name)
    elseif choice == all_choices[2] then
      player:broadcastSkillInvoke(fudi.name, 2)
      room:notifySkillInvoked(player, fudi.name, "offensive")
      room:damage{
        damage = double and 2 or 1,
        to = target,
        from = player,
        skillName = fudi.name,
      }
    end
  end
end

fudi:addEffect("active", {
  mute = true,
  prompt = "#swis__fudi-prompt",
  can_use = function (self, player)
    return player:usedSkillTimes(fudi.name, Player.HistoryPhase) == 0
  end,
  card_num = 1,
  target_num = 1,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function (self, room, effect)
    fudi_effect(room, effect.from, effect.tos[1], effect.cards[1])
  end
})

fudi:addEffect(fk.Damaged, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(fudi.name) and player == target
    and not player:isKongcheng() and data.from and data.from ~= player
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      skill_name = fudi.name,
      cancelable = true,
      include_equip = false,
      prompt = "#swis__fudi-trigger:"..data.from.id
    })
    if #cards == 1 then
      event:setCostData(self, cards[1])
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    fudi_effect(player.room, player, data.from, event:getCostData(self), true)
  end
})

return fudi